
//全部拆线
$("#DisconnectAll").click(function () {

    $.bc({
        method: "post",
        url: "iifoxHandler/ActiveCalls",
        data: { uuid: "#" }, //uuid为#则为全部拆线
        callback: function (result) {
            if (result == false) {
                swal({
                    html: true,
                    title: "拆线失败",
                    text: "拆线失败,请重试！",
                    type: "error",
                    showCancelButton: true,
                    cancelButtonClass: 'btn-secondary',
                    confirmButtonText: "确认",
                    confirmButtonClass: "btn-danger ml-2",
                    cancelButtonText: "取消"

                });
            }
            else {
                swal({
                    html: true,
                    title: "拆线成功",
                    text: "拆线成功",
                    type: "info",
                    showCancelButton: true,
                    cancelButtonClass: 'btn-secondary',
                    confirmButtonText: "确认",
                    confirmButtonClass: "btn-danger ml-2",
                    cancelButtonText: "取消"

                });
            }
        }
    });

})

$(function () {

    Tabs(); //Table绑定
    setInterval(function () {  //每s刷新Table
        getDate();//赋值当前时间

    }, 1000);


    setInterval(function () {  //每s刷新Table

        //$('#DataGrid').bootStrapTable('getOptions').pageNumber
        $("#DataGrid").bootstrapTable("refresh", {
            silent: true //静态刷新
        });
        $("#callnum").html($("#DataGrid").bootstrapTable("getData").length); //赋值当前通话数
        call();//最大呼入呼出量
    }, 5000);
});

//获取当前时间
function getDate() {
    var myDate = new Date().Format("yyyy/MM/dd hh:mm:ss"); //获取系统当前时间
    $("#iTime").html(myDate);//赋值当前时间

}

//最大呼入呼出量
function call() {
    var rows = $('#DataGrid').bootstrapTable('getData');//行的数据
    if (rows.length > 0) { //当Table中有数据时
        var InCall = 0; //呼入量
        var OutCall = 0; //呼出量
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].CallDirection == "inbound") //如果为呼入
                InCall += 1;
            else if (rows[i].CallDirection == "outbound") //如果为呼出
                OutCall += 1;
        }
        $.bc({
            url: "iifoxHandler/ActiveCalls",
            method: "put",
            data: { InCall: InCall, OutCall: OutCall },
            callback: function (result) {
                if (result == false) {
                }
                else {
                }
            }
        })
    }
}

//tab绑定
function Tabs() {
    var c_url = "iifoxHandler/ActiveCalls";
    var $table = $('#DataGrid');
    var $dialogNew = $('#dialogNew');
    $table.lgbTable({

        url: c_url,

        dataBinder: {
            map: {
                uuid: "#uuid", //uuid
                dest: "#dest",//分机
            },
            callback: function (data) {
            }
        },
        smartTable: {
            idField: "uuid",
            sortName: "created",
            sortOrder: "desc",
            queryParams: function (params) {
                return $.extend(params, {

                });
            },           //传递参数（*）
            columns: [
                {
                    title: "呼叫参考号", field: "uuid", sortable: true
                },
                { title: "主叫号", field: "cid_num", sortable: true, class: "text-center" },
                { title: "分机号", field: "callee_num", sortable: true, class: "text-center" },
                { title: "被叫号", field: "dest", sortable: true, class: "text-center" },
                { title: "当前状态", field: "state", sortable: true, class: "text-center" },
                { title: "呼叫方向", field: "CallDirection", sortable: true, class: "text-center" },
                { title: "开始时间", field: "created", sortable: true, class: "text-center" },
                {
                    title: "持续时间", field: "TIME", sortable: true, class: "text-center", formatter: function (value, row, index) {
                        var _Date = new Date().Format("yyyy-MM-dd hh:mm:ss");//当前时间
                        var _S = (new Date(_Date) - new Date(row.created)) / 1000;//取秒数
                        return newDate(_S);//转换格式： hh：MM：dd
                    }
                },
            ],
            editButtons: {
                events: {
                    'click': function (e, value, row, index) {
                        $table.bootstrapTable('uncheckAll');
                        $table.bootstrapTable('check', index);
                    }, 'click .stop': function (e, value, row, index) {//拆线

                        $.bc({
                            url: "iifoxHandler/ActiveCalls",
                            method: "post",
                            data: { uuid: row.uuid },
                            callback: function (result) {
                                if (result == false) {
                                    swal({
                                        html: true,
                                        title: "拆线失败",
                                        text: "拆线失败,请重试！",
                                        type: "error",
                                        showCancelButton: true,
                                        cancelButtonClass: 'btn-secondary',
                                        confirmButtonText: "确认",
                                        confirmButtonClass: "btn-danger ml-2",
                                        cancelButtonText: "取消"

                                    });
                                }
                                else if (result == true) {
                                    swal({
                                        html: true,
                                        title: "拆线成功",
                                        text: "拆线成功",
                                        type: "info",
                                        showCancelButton: true,
                                        cancelButtonClass: 'btn-secondary',
                                        confirmButtonText: "确认",
                                        confirmButtonClass: "btn-danger ml-2",
                                        cancelButtonText: "取消"

                                    });
                                }
                            }
                        })
                    },
                    'click .Toagent': function (e, value, row, index) { //转入座席
                        $("#uuid").val(row.uuid);
                        $dialogNew.modal('show');
                    }
                },
                formatter: function (value, row, index) {
                    var $this = this.clone();
                    if (row.IsReset === 0) {
                        $this.find('button.reset').remove();
                    }
                    return $this.html();
                }
            },
            exportOptions: {
                fileName: "活动呼叫",
                ignoreColumn: [0, 7]
            }
        }
    });
}


function newDate(value) { //将秒数转为时分秒 格式 HH:mm:ss
    if (value == null || value == "") { //如果value 为空  返回空
        return "";
    }
    value = parseInt(value); //去掉小数点后数据
    var data;
    if (value > -1) {
        var hour = Math.floor(value / 3600);//小时
        var min = Math.floor(value / 60) % 60;//分钟
        var sec = value % 60;
        if (hour < 10) {
            data = '0' + hour + ":";
        } else {
            data = hour + ":";
        }
        if (min < 10) { data += "0"; }
        data += min + ":";
        if (sec < 10) { data += "0"; }
        data += sec.toFixed(0);
    }
    return data;
}

// 对Date的扩展，将 Date 转化为指定格式的String 
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1,                 //月份 
        "d+": this.getDate(),                    //日 
        "h+": this.getHours(),                   //小时 
        "m+": this.getMinutes(),                 //分 
        "s+": this.getSeconds(),                 //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds()             //毫秒 
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
